﻿using Blazui.Component;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using Project13776616780_01.Model;
using Project13776616780_01.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Project13776616780_01.Pages.Author
{
    public partial class Create
    {
        private BInput<string> editor;
        private BForm form;
        [Inject]
        private DocsDbContext dbContext { get; set; }

        private async Task CreateAsync()
        {
            if (!form.IsValid())
            {
                return;
            }
            var description = await JSRuntime.InvokeAsync<string>("SyncEditor");
            var model = form.GetValue<AuthorModel.CreateModel>();
            dbContext.Add(new Model.Author()
            {
                Description = description,
                Name = model.Name,
                Age = model.Age
            });
            await dbContext.SaveChangesAsync();
            await CloseAsync();
        }
        protected override void OnAfterRender(bool firstRender)
        {
            base.OnAfterRender(firstRender);
            if (firstRender)
            {
                _ = JSRuntime.InvokeVoidAsync("registerHtmlEditor", editor.InputElement);
            }
        }
    }
}
